<?php
include('config.php');

function saveRide($rideName,$userId){
	$insertRide = 'insert into ride(creator,name,creation_date) values('.$userId.',"'.$rideName.'","'.date('Y-m-d').'")' or die ("erreur d'enregistrement Ride");
	mysql_query($insertRide);
	return mysql_insert_id();
}

function savePoint($lat,$lon,$numSeq,$idRide){
	$insertPoint = "insert into point(latitude,longitude,numSeq,ride) values ($lat,$lon,$numSeq,$idRide)" or die ("erreur d'enregistrement Point");
	mysql_query($insertPoint);
}

if(isset($_FILES['userFile'],$_SESSION['userid'])){

	$userId = $_SESSION['userid'];
	$upload = (object) $_FILES['userFile'];

	if($upload->error == UPLOAD_ERR_OK)	{

		$xml = simplexml_load_file($upload->tmp_name);
		$array = explode('.',$upload->name);
		$extension = $array[1];
		
		if($extension == "gpx"){
			$rideName = $xml->trk->name;
			$idRide = saveRide($rideName,$userId);

			$numSeq = 0;
			$arrayLat = array();
			$arrayLon = array();
			foreach($xml->trk->trkseg->trkpt as $point){
				array_push($arrayLat,(double)$point['lat']);
				array_push($arrayLon,(double)$point['lon']);
				savePoint($point['lat'], $point['lon'], $numSeq, $idRide);
				$numSeq++;
			}

		}
	}else{
		switch($_upload->error){
			case UPLOAD_ERR_INI_SIZE:
				echo "The uploaded file exceeds the upload_max_filesize directive in php.ini";
				break;
			case UPLOAD_ERR_FORM_SIZE:
				echo "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
				break;
			case UPLOAD_ERR_PARTIAL:
				echo "The uploaded file was only partially uploaded";
				break;
			case UPLOAD_ERR_NO_FILE:
				echo "No file was uploaded";
				break;
			case UPLOAD_ERR_NO_TMP_DIR:
				echo "Missing a temporary folder";
				break;
			case UPLOAD_ERR_CANT_WRITE:
				echo "Failed to write file to disk";
				break;
			case UPLOAD_ERR_EXTENSION:
				echo "File upload stopped by extension";
				break;
			default:
				echo "Unknown upload error";
				break; 
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>
		Share my ride
	</title>
	<!-- La feuille de styles "base.css" doit être appelée en premier. -->
	<link rel="stylesheet" type="text/css" href="styles/base.css" media="all" />
	<link rel="stylesheet" type="text/css" href="styles/modele.css" media="screen" />
	<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<html>
<head>
<?php include'head.php';?>
</head>
<body>
		<div id="global">
				<?php	include('menu.php'); ?>

				<div id="centre">

						<div id="principal">

								<form enctype="multipart/form-data" action="importRide.php" method="post">
									<!-- MAX_FILE_SIZE doit précéder le champ input de type file (défini la taille maximum acceptée ici ~15 Mo) -->
									<input type="hidden" name="MAX_FILE_SIZE" value="15000000" />
									<!-- Le nom de l'élément input détermine le nom dans le tableau $_FILES -->
									Importer un parcours : <input name="userFile" type="file" />
									<input type="submit" value="Envoyer le fichier" />
								</form>
				
								<?php include('googleMap.php'); ?>

						</div><!-- #principal -->

						<div id="secondaire">
							<?php include 'col_gauche.php'; ?>
						</div><!-- #secondaire -->

				</div><!-- #centre -->

		</div><!-- #global -->

		<div class="foot"><a href="<?php echo $url_home; ?>">Retour &agrave; l'accueil</a></div>

</body>
</html>
